var	url_satellite='http://{s}.tiles.mapbox.com/v3/dennisl.map-6g3jtnzm/{z}/{x}/{y}.png',
	url_mapQuest = 'http://otile3.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg',
	url_OSMtransport = 'http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png',
	url_OSMstandart = 'http://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png',

	transport = new L.TileLayer(url_OSMtransport, {minZoom: 12, maxZoom: 18,
		attribution: ''}),
	standart = new L.TileLayer(url_OSMstandart, {minZoom: 12, maxZoom: 18,
		attribution: ''}),
	satellite = new L.TileLayer(url_satellite, {minZoom: 12, maxZoom: 18,
		attribution: ''}),
	mapQuest = new L.TileLayer(url_mapQuest, {minZoom: 12, maxZoom: 18,
		attribution: ''}),
	mapQuest2 = new L.TileLayer(url_mapQuest, {minZoom: 9, maxZoom:15});

var baseMaps = {
	'OSM Transport':transport,
	'MapQuest':mapQuest,
	'OSM Standart':standart,
	'Mapbox Satellite': satellite
};

var map = new L.Map('MainMap', {
	    layers: [mapQuest],
	    center: [10.772113,106.657832],
	    zoom: 16,
		zoomControl: false,
		contextmenu: false,
		contextmenuWidth: 160,
		contextmenuItems: [{
		text: 'Tìm đường từ đây',
	}, '-', {
		text: 'Tìm đường đến đây',
	}, {
		text: 'Chia sẻ địa điểm này',
	}, {
		text: 'Lưu lại vị trí này',
	}]
});

map.addControl(L.control.zoom({position: 'topright'}));
	
var LeafIcon = L.Icon.extend({
	options: {
	    iconUrl: '/public/plugin/images/marker-icon.png',
	    //iconSize: [50, 50],
	    iconAnchor: [12, 40],
	    popupAnchor: [0, -56],
	    shadowSize: [68, 95],
	    shadowAnchor: [22, 94]
	}
});

function getJourney(){
	$.ajax({
	  url: 'ajax',
	  type: 'GET',
	  dataType: 'json',
	  data : {
			"journey_id" : journey_id
		},
	  success: function(response) {
		
		var a = response;
		console.log(a);
		var j ;
		var mapIcon = new LeafIcon();
		for (var i=0;i<a.length-1;i++){
			j=i+1;
			if (a[i].flag==0){
				L.marker([parseFloat(a[i].latitude),parseFloat(a[i].longtitude)], {icon: mapIcon}).addTo(map).bindPopup('Start here').openPopup();
			}
			if (j==a.length-1){
				L.marker([parseFloat(a[j].latitude),parseFloat(a[j].longtitude)], {icon: mapIcon}).addTo(map).openPopup();
			}
			L.polyline([[parseFloat(a[i].latitude),parseFloat(a[i].longtitude)],[parseFloat(a[j].latitude),parseFloat(a[j].longtitude)]], {color: 'red' , weight:8}).addTo(map);
		}
		var index = parseInt(a.length/2)-1;
		map.setView(new L.LatLng(a[index].latitude, a[index].longtitude));
	  },
	  error: function(e) {
		//called when there is an error
		//console.log(e.message);
	  }
	});
	
}
getJourney();
